<?php

/**
 * JS validator,Copyright (c) 2008 Arian Stolwijk, <http://aryweb.nl>, MIT Style License.
 */

include_once 'fh3/class.FormHandler.php';

class Fh3_JsValidator extends FormHandler {

	protected $jsvalidator;

	public function __construct($name=null,$action=null,$extra=null,$jsvalidator=true){
		parent::FormHandler($name,$action,$extra);
		
		$this->jsvalidator = $jsvalidator;
	}

	function _setJS( $js, $isFile = false, $before = true, $eachform = false)
	{
		$this->_js[$before?0:1][$isFile?'file':'code'][] = $js;
		if(!$isFile){
			$this->_js[$before?0:1]['eachform']['code'][] = $eachform;
		}else{
			$this->_js[$before?0:1]['eachform']['file'][] = $eachform;
		}
	}
	
	function getJavascriptCode( $header = true )
	{
		static $return = array( 0 => false, 1 => false );;

		$s = $header ? 0 : 1;

		// if the javascript is not retrieved yet..
//		if( !$return[$s] )
//		{
			// generate the js "files" script
			$result = '';
			if( isset($this->_js[$s]['file']) && is_array($this->_js[$s]['file']) )
			{
				foreach( $this->_js[$s]['file'] as $key => $line )
				{
					if($this->_js[$s]['eachform']['file'][$key] || !$return[$s]){
						$result .= '<script language="javascript" type="text/javascript" src="'.$line.'"></script>'."\n";
					}
				}
			}
			
			// generate the other js script
			if( isset($this->_js[$s]['code']) && is_array($this->_js[$s]['code']) )
			{
				$jscode = array();
				foreach( $this->_js[$s]['code'] as $key => $code){
					if($this->_js[$s]['eachform']['code'][$key] || !$return[$s]){
						$jscode[] = $code;
					}
				}
				if(count($jscode) >= 1){
					$result .= '<script language="javascript" type="text/javascript">'."\n";
					$result .= implode($jscode);
					$result .= "</script>\n";
				}
			}
			
			$return[$s] = true;
			return $result;
//		}

		return '';
	}
	
	
	public function getFields(){
		$fields = array();
		
		foreach($this->_fields as $key => $field){
			if(!empty($key)){
				$fields[] = $key;
			}
		}
		return $fields;
	}
	
	public function getField($fldName){
		if(!empty($this->_fields[$fldName])){
			return $this->_fields[$fldName][1];
		}else{
			trigger_error('Dit veld bestaat niet');
		}
	}
	
	public function flush($return = false){
		$validator = array();
		if($this->jsvalidator){
			foreach($this->getFields() as $field){
				$field = $this->getField($field);
				
				if(!empty($field->_sValidator)){
					$validator[$field->_sName] = $field->_sValidator;
				}
			}

			$this->_setJS('validatorPacked.js',true,true);
			static $validatorI = 0;
			$js = '	var Validator'.++$validatorI.' = new jsValidator();'.PHP_EOL.
				"	Validator".$validatorI.".settings.msg = '".$this->_text( 14 )."';".PHP_EOL.
				'	Validator'.$validatorI.'.showMsg = function(elmt,elem){'.PHP_EOL.
				"		document.getElementById('error_'+elem).innerHTML = '';".PHP_EOL.
				'		if (elmt) {'.PHP_EOL.
				"			elmt.style.display = 'inline';".PHP_EOL.
				'		}'.PHP_EOL.
				'	}'.PHP_EOL.
				
				'	Validator'.$validatorI.'.hideMsg = function(elmt,elem){'.PHP_EOL.
				"		document.getElementById('error_'+elem).innerHTML = '';".PHP_EOL.
				'		if (elmt) {'.PHP_EOL.
				"			elmt.style.display = 'none';".PHP_EOL.
				'		}'.PHP_EOL.
				'	}'.PHP_EOL.
				
				'	// Add the elements to the validator'.PHP_EOL.
				'	Validator'.$validatorI.'.elmts = {'.PHP_EOL;
			
			$aantal = count($validator);
			$i=1;
			foreach($validator as $field => $fldValidator){
				$js .= '		'.$field.': {'.PHP_EOL.
					"			validator: '".$fldValidator."'".PHP_EOL.
					'		}'.($i++ < $aantal ? ',':'').PHP_EOL;
			}
			$js .= '	}'.PHP_EOL.
			'	Validator'.$validatorI.'.init();'.PHP_EOL;
			$this->_setJS($js,false,false,true);
		}
		
		parent::flush($return);
	}
	
}
